package com.hngy.xpq.android_practice.ui.notifications;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.adapter.OrderAdapter;
import com.hngy.xpq.android_practice.bean.Orders;
import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.database.SQLiteOrder;
import com.hngy.xpq.android_practice.database.SQLiteWallet;
import com.hngy.xpq.android_practice.ui.home.User_Comm;

import java.util.List;

public class NotificatiosOrder extends AppCompatActivity {

    SQLiteOrder sqLiteOrder;
    SQLiteWallet sqLiteWallet;
    Wallet w;
    ListView order;
    List<Orders> list;
    OrderAdapter orderAdapter;
    SharedPreferences main,merchant;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_notificatios);

        //调用本地存储
        main = super.getSharedPreferences("main",MODE_PRIVATE);
        merchant = super.getSharedPreferences("merchants",MODE_PRIVATE);

        order = findViewById(R.id.order_list);
        OrderData();

        order.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Orders o = list.get(position);
                if (main.getString("name","").equals(o.getUser_name())){
                    if (o.getStatus().equals("等待派送")){
                        Toast.makeText(NotificatiosOrder.this,"请等待商家派送!!",Toast.LENGTH_SHORT).show();
                        return;
                    }else if(o.getStatus().equals("签收成功")){
                        Toast.makeText(NotificatiosOrder.this,"已签收!!",Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        if (sqLiteOrder.UpdateOrder(o.getId(),"签收成功")){
                            Toast.makeText(NotificatiosOrder.this,"签收成功!!",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(NotificatiosOrder.this,NotificatiosOrder.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(NotificatiosOrder.this,"签收失败!!",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                } else {
                    if (o.getStatus().equals("等待派送")){
                        if (sqLiteOrder.UpdateOrder(o.getId(),"派送中")){
                            Toast.makeText(NotificatiosOrder.this,"派送成功!!",Toast.LENGTH_SHORT).show();
                            //商家收帐
                            w = new Wallet();
                            w.setName(merchant.getString("phone",""));
                            w.setSingle_money(o.getComm_price());
                            w.setDes("收到买家:" + o.getUser_name() + "付款:" + o.getComm_price() + "元");
                            if (sqLiteWallet.recharge(w)){
                                Toast.makeText(NotificatiosOrder.this,"收款成功!!",Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(NotificatiosOrder.this,"系统出现故障，请稍后再试!!",Toast.LENGTH_SHORT).show();
                            }
                            Intent intent = new Intent(NotificatiosOrder.this,NotificatiosOrder.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(NotificatiosOrder.this,"派送失败!!",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else if (o.getStatus().equals("派送中")){
                        Toast.makeText(NotificatiosOrder.this,"请等待买家签收!!",Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        Toast.makeText(NotificatiosOrder.this,"订单完成!!",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            }
        });
    }

    private void OrderData() {
        sqLiteOrder = new SQLiteOrder(this);
        sqLiteWallet = new SQLiteWallet(this);
        showQueryData();
    }

    private void showQueryData() {
        if (list != null) {
            list.clear();
        }
        if (main.getString("name","").equals(""))
            list = sqLiteOrder.OrdersQuery(merchant.getString("name",""));
        else
            list = sqLiteOrder.OrdersUserQuery(main.getString("name",""));
        orderAdapter = new OrderAdapter(this,list);
        order.setAdapter(orderAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }
}